<template>
    <div class="publishPassport" v-show="$store.state.publishShow">
        
        <div class="publishPassport-inner">
        <div class="top" style="margin-top: 10px;">
           <h3 style="display: inline;">发布文章</h3>
          <!-- <img src="../../assets/icon/x-lg.svg" alt="关闭" style="cursor: pointer;margin:10px 10px; display: block;float: right;" class="close"> -->
          <el-button icon="el-icon-close" style=" padding: 0px; border: none;float: right;" class="close"></el-button>
        </div>
        <div class="pctrANDsummer">
           <div class="label">
              <p>封面&摘要:</p>

           </div>
           <div class="label-content">
                <el-radio v-model="coverRadio" label="1">单图</el-radio>
                <el-radio v-model="coverRadio" label="2">无封面</el-radio>
              
             <div class="content-inner">
                  <div class="picture" v-show="coverRadio =='1'">
                    <el-upload
                    class="avatar-uploader"
                    action="https://jsonplaceholder.typicode.com/posts/"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                  </el-upload>

                  </div>
                  <div class="summerize"> 
                  <el-input
                  type="textarea"
                  placeholder="摘要（必填）：会在推荐、列表等场景外露，帮助读者快速了解内容，支持一键将正文前 256 字符键入摘要文本框"
                  v-model.trim="summarize"
                  maxlength="256"
                  show-word-limit
                  style="margin-top: 20px;"
                >
                </el-input>
                  </div>
              </div> 
           </div>
        </div>
        <div class="tabs">
            <div class="tabs-article">
              <p style="display: inline-block;">文章标签:</p>
              <el-tag
                :key="tag"
                v-for="tag in dynamicTags"
                closable
                :disable-transitions="false"
                @close="handleClose(tag)">
                {{tag}}
              </el-tag>
              <el-input
                class="input-new-tag"
                v-if="inputVisible"
                v-model.trim="inputValue"
                ref="saveTagInput"
                size="small"
                @keyup.enter.native="handleInputConfirm"
                @blur="handleInputConfirm"
              >
              </el-input>
              <el-button v-else class="button-new-tag" size="small" @click="showInput" style="margin-top: 6px;">+ 添加文章标签</el-button>
            </div>

            <div class="showWho">
              <p style="display: inline-block;margin-right: 20px;">发布形式:</p>
               <el-radio v-model="sendwhoRadio" label="0">全部可见</el-radio>
                <el-radio v-model="sendwhoRadio" label="1">仅我可见</el-radio>
                <el-radio v-model="sendwhoRadio" label="2">粉丝可见</el-radio>
            </div>


        </div>
        
        <div class="buttons">
          <el-row>
            <el-button class="btn" type="danger" @click="TuploadArticle">发布文章</el-button>
            <el-button class="btn" plain @click="TsavaPage">保存草稿</el-button>
            <el-button class="btn cancel" plain><span class="cancel">取消</span></el-button>
          </el-row>

        </div>
        </div>

    </div>
</template>

<script>
import {savapage} from "@/api/article"
import {uploadArticle} from "@/api/article"
import SnowflakeId from "snowflake-id";
import { Message } from 'element-ui';
const snowflake = new SnowflakeId({
    mid: 42,
    offset: (2019 - 1970) * 31536000 * 1000
});

export default {
  name:"PublishPassport",
  data() {
    return {
      coverRadio: '2',
      sendwhoRadio:"0",
      summarize:"",
      //上传路径
      imageUrl: '',
      //关于标签的
      dynamicTags: [],
        inputVisible: false,
        inputValue:""
    }
  },
  props:["article"],
  methods: {
    //关于上传图片的操作
      handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        // const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        // if (!isJPG) {
        //   this.$message.error('上传头像图片只能是 JPG 格式!');
        // }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        // return isJPG && isLt2M;
        return isLt2M;
      },
    //关于标签的操作
      handleClose(tag) {
        this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
      },

      showInput() {
        if(this.dynamicTags.length<6){
          this.inputVisible = true;
         this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus();
        });
        }
     
      },

      handleInputConfirm() {
        let inputValue = this.inputValue;
        if (inputValue) {
          this.dynamicTags.push(inputValue);
        }
        this.inputVisible = false;
        this.inputValue = '';
      },
      TsavaPage(){
        var articleid = snowflake.generate();
        this.article.tags=this.dynamicTags;
        this.article.sendwho=this.sendwhoRadio;
        this.article.summarize=this.summarize;
        if(this.article.articleId==""){
          this.article.articleId=articleid;
        }
        savapage(this.article);
           
      },
      TuploadArticle(){
        var articleid = snowflake.generate();
        this.article.tags=this.dynamicTags;
        this.article.sendwho=this.sendwhoRadio;
        this.article.summarize=this.summarize;
        if(this.article.articleId==""){
          this.article.articleId=articleid;
        }
        if(this.summarize==""){
          Message.info("摘要不能空")
        }else{
          uploadArticle(this.article).then(res=>{
          if(res){
            setTimeout(()=>{
              this.$store.commit("PUBLISHSHOW",false) 
            },800)
          }
        });

        }
      

      }

    }
   
}
</script>

<style>
*{
  margin: 0;
}
textarea{
  resize: none !important;
  height: 100px;
  font-size: small;
  overflow: hidden;
  

}
.content-inner{
  display: flex;
  width: 100%;
  height: 130px;

}
.picture{
  flex-grow: 1;



}
.summerize{
  flex-grow: 11;


}
::-webkit-input-placeholder { 

opacity: 1;
} 
.publishPassport{
position: absolute;
margin: auto;
left: 25%;
top: 15%;
width: 650px;
height: 600px;
z-index: 8;
border-radius:0.7ch ;
background-color:white;
overflow: hidden;
display: flex;
justify-content: center;
align-items: center;
}
.publishPassport-inner{
   /* background-color: rgb(36, 18, 105); */
   width: 90%;
   height: 95%;
}
.pctrANDsummer{
  display: flex;
  margin-top: 40px;
  width: 100%;
  height: 150px;

}
.label{
  padding-top: 2px;
  flex-grow: 1;
  font-size: small;
  height: 95%;
}
.label-content{

  flex-grow: 7;
}
.avatar-uploader .el-upload {
    margin-top: 20px;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 125px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 125px;
    display: block;
  }
  .tabs{
    margin-top: 10px;
    width: 100%;
    height: 80px;
    /* background-color: #409EFF; */
  }
  .tabs-article{
    width: 100%;
    height: 40px;
    line-height: 40px;
  }
  .showWho{
    margin-top: 20px;
    width: 100%;
    height: 40px;
    line-height: 40px;
  }
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
  .buttons{
    margin-top: 50px;
    width: 100%;
    height: 80px;
    padding-top: 30px;
  }
  .btn{
    margin-left: 20px;
    float: right;
  }


</style>